package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;


    @Override
    public void saveStudent(Student student) {
        List<Clazz> list = clazzService.list(new LambdaQueryWrapper<Clazz>()
                .eq(student.getClazzId() != null, Clazz::getId, student.getClazzId()));
        student.setClazzName(list.get(0).getName());
        this.save(student);
    }

    @Override
    public void violationCount(Integer id, Integer violationScore) {
        //现根据id查询出来学生
        Student student = this.getById(id);
        student.setViolationCount((short)(student.getViolationCount() + 1));
        student.setViolationScore((short)(student.getViolationScore() + violationScore));
        this.updateById(student);
    }


}
